﻿using Core.Model;
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using Core.Extension;
using Dapper;
using System.Linq.Expressions;
using CoreSolution.Tools;

namespace Core
{
   public abstract class SqlProvider<T>
    {
        /// <summary>
        /// 条件控制
        /// </summary>
        public SetContext SetContext { get; set; }
        
        /// <summary>
        /// 链接字符串
        /// </summary>
        public string ConnectStr { get; set; }


        public SqlProvider()
        {
            ConnectStr = ConfigHelper.GetSectionValue("ConnectStr");
        }



        #region 组建 Lambda 拓展

        /// <summary>
        /// 组建 ID
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        protected static Expression<Func<T, bool>> CreateEqualityExpressionForId(Guid Id)
        {
            var lambdaParam = Expression.Parameter(typeof(T));

            var lambdaBody = Expression.Equal(
                Expression.PropertyOrField(lambdaParam, "Id"),
                Expression.Constant(Id, typeof(Guid))
                );

            return Expression.Lambda<Func<T, bool>>(lambdaBody, lambdaParam);
        }


        /// <summary>
        /// 组建 IsDelete
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        protected static Expression<Func<T, bool>> CreateEqualityExpressionForIsDelete(bool IsDelete)
        {
            var lambdaParam = Expression.Parameter(typeof(T));

            var lambdaBody = Expression.Equal(
                Expression.PropertyOrField(lambdaParam, "IsDelete"),
                Expression.Constant(IsDelete, typeof(bool))
                );
            return Expression.Lambda<Func<T, bool>>(lambdaBody, lambdaParam);
        }

        /// <summary>
        /// 组建 UpdateId
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        protected static Expression<Func<T, T>> CreateEqualityExpressionForUpdateId(Guid? UpdateId)
        {
            var lambdaParam = Expression.Parameter(typeof(T));

            var lambdaBody = Expression.Equal(
                Expression.PropertyOrField(lambdaParam, "UpdateId"),
                Expression.Constant(UpdateId, typeof(Guid?))
                );
            return Expression.Lambda<Func<T, T>>(lambdaBody, lambdaParam);
        }

        /// <summary>
        /// 组建 UpdateDate
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        protected static Expression<Func<T, T>> CreateEqualityExpressionForUpdateDate(DateTime? UpdateDate)
        {
            var lambdaParam = Expression.Parameter(typeof(T));

            var lambdaBody = Expression.Equal(
                Expression.PropertyOrField(lambdaParam, "UpdateDate"),
                Expression.Constant(UpdateDate, typeof(DateTime?))
                );
            return Expression.Lambda<Func<T, T>>(lambdaBody, lambdaParam);
        }
        #endregion
    }
}
